<?php

if (!defined('BASEPATH'))
    exit('No direct script aceess allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * @Author Joydeb Choudhury(macjoydeb@gmail.com)
 */

class Management extends MY_Controller {

    function management() {
        parent :: __construct();
        $this->template->set_theme('admin');
        $this->template->set_layout('admin_layout');
        $this->load->model('counter_model');
        $this->load->model('pickup_model');
        $this->load->library('pagination');
        $this->load->model('management_model');
        $this->commoncall->check_admin_login();
    }
//------------Get all make or Company ---------
    function make() {
        $per_page = 10;
        $offset = 0 + $this->uri->segment(4);
        $config['base_url'] = base_url() . 'admin/management/make/';
        $config['total_rows'] = $this->counter_model->make_count('CAR');
        $config['uri_segment'] = '4';
        $config['full_tag_open'] = '';
        $config['full_tag_close'] = '';
        $this->pagination->initialize($config);
        $data['make'] = $this->management_model->get_makes('CAR', $per_page, $offset);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_make', $data);
    }
//------------Make or Company Insert ---------
    function make_insert() {
        if ($this->input->post('insert')) {
            $company_detail = $_POST['company'];
            $company_link = $_POST['link'];
            $this->form_validation->set_rules('company', 'company', 'required|xss_clean|is_unique[make.make]');
            $this->form_validation->set_rules('link', 'link', 'xss_clean');
            if ($this->form_validation->run()) {
                if ($company_detail != 'Enter Company')
                    $this->management_model->make_insert('CAR', $company_detail, $company_link);
                else
                    $this->session->set_flashdata('message', 'Type a Company Name');
                redirect('admin/management/make');
            } else {
                $this->session->set_flashdata('message', 'Company already exists');
                redirect('admin/management/make');
            }
        }
    }
//------------Make or Company Update ---------
    function make_update() {
        $company_id = $_POST['make_id'];
        $company_detail = $_POST['make_detail'];
        $company_link = $_POST['link'];
        $this->form_validation->set_rules('make_id', 'make_id', 'required|xss_clean');
        $this->form_validation->set_rules('make_detail', 'make_detail', 'xss_clean');
        $this->form_validation->set_rules('link', 'link', 'xss_clean');
        if ($this->form_validation->run()) {
            $this->management_model->make_update('CAR', $company_id, $company_detail, $company_link);
        }
        redirect('admin/management/make');
    }
//------------Make or Company logo upload form ---------
    function make_logo_uplaod($makeID) {
        $data['company_id'] = $makeID;
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_make_logo_upload', $data);
    }
//------------Make or Company logo upload ---------
    public function make_logo() {
        $make_id = $_POST['make_id'];
        $company_detail = strtolower($_POST['make_detail']);
        $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['makelogo']['name']);
        $logoname = url_title($company_detail, '_') . '.' . $ext;
        $filetype = $_FILES['makelogo']['type'];
        if ($_FILES['makelogo']['error'] == 0 && (($filetype == 'image/jpeg') || ($filetype == 'image/jpg') || ($filetype == 'image/png') || ($filetype == 'image/gif'))) {
            $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['makelogo']['name']);
            $filename = $logoname . '.' . $ext;
            $config['file_name'] = $logoname;
            $config['upload_path'] = './assets/images/data/car/company/';
            $config['allowed_types'] = 'gif|png|jpeg|jpg';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            //--------delete previous logo -------
            $logo = $this->management_model->get_make_image('CAR', $make_id);
            if ($logo != '') {
                $path = BASEPATH . '../assets/images/data/car/company/' . $logo;
                unlink($path);
            }
            //--------update record-----------
            $data = array('image' => $logoname);
            $this->db->where('id', $make_id);
            $this->db->where('type', 'CAR');
            $this->db->update('make', $data);
            //----upload logo---------------
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('makelogo')) {
                $this->session->set_flashdata('message', $this->upload->display_errors('<p class="error">', '</p>'));
            }
            redirect('admin/management/make_logo_uplaod/' . $make_id);
        }
        $this->session->set_flashdata('message', "No file choosen! Plaese choose a file only (jpg, gif, png ,jpeg)");
        redirect('admin/management/make_logo_uplaod/' . $make_id);
    }

//--Compnay or Make delete ---//
    function make_delete($make_id) {
        //--------delete previous logo from make -------
        $logo = $this->management_model->get_make_image('CAR', $make_id);
        if ($logo != '') {
            $path = BASEPATH . '../assets/images/data/car/company/' . $logo;
            unlink($path);
        }

        $sql = "select image from version where make_id=$make_id";
        $query = $this->db->query($sql);
        $result = $query->result();

        //--------delete previous logo from version -------

        foreach ($result as $result):
            $logo = $result->image;
            if ($logo != '') {
                $path = BASEPATH . '../assets/images/data/car/version/' . $logo;
                unlink($path);
            }
        endforeach;

        $this->management_model->make_delete('CAR', $make_id);
        redirect('admin/management/make');
    }
//---------All models --------------
    function model() {
        $company_id = $this->uri->segment(4);
        $per_page = 10;
        $offset = 0 + $this->uri->segment(5);
        $config['base_url'] = base_url() . 'admin/management/model/' . $company_id;
        $config['total_rows'] = $this->counter_model->model_count('CAR', $company_id);
        $config['uri_segment'] = '5';
        $config['full_tag_open'] = '';
        $config['full_tag_close'] = '';
        $this->pagination->initialize($config);
        $data['company_id'] = $company_id;
        $data['model'] = $this->management_model->get_models('CAR', $per_page, $offset, $company_id);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_model', $data);
    }
//------------Model update--------------
    function model_update() {
        $model_detail = $_POST['model_detail'];
        $model_id = $_POST['model_id'];
        $model_detail = $_POST['model_detail'];
        $company_id = $_POST['make_id'];
        $this->form_validation->set_rules('model_id', 'model_id', 'required|xss_clean');
        $this->form_validation->set_rules('model_detail', 'model_detail', 'required|xss_clean');
        if ($this->form_validation->run()) {
            $this->management_model->model_update('CAR', $model_id, $model_detail);
        }
        redirect('admin/management/model/' . $company_id);
    }
//------------Model insert--------------
    function model_insert() {
        if ($this->input->post('insert')) {
            $model_detail = $_POST['model'];
            $company_id = $_POST['company_id'];
            $this->form_validation->set_rules('model', 'model', 'required|xss_clean|is_unique[model.model]');
            if ($this->form_validation->run()) {
                if ($model_detail != 'Enter Model')
                    $this->management_model->model_insert('CAR', $model_detail, $company_id);
                else
                    $this->session->set_flashdata('message', 'Type a model name');
            } else {
                $this->session->set_flashdata('message', 'Model already exists');
            }
        }
        redirect('admin/management/model/' . $company_id);
    }

    //-- Model Delete
    function model_delete($modelID, $makeID) {
        $this->management_model->model_delete('CAR', $modelID);
        redirect('admin/management/model/' . $makeID);
    }
//------------All Version--------------
    function version($makeID, $modelID) {
        $per_page = 10;
        $offset = 0 + $this->uri->segment(6);
        $config['base_url'] = base_url() . 'admin/management/version/' . $makeID . '/' . $modelID;
        $config['total_rows'] = $this->counter_model->version_count('CAR', $modelID);
        $config['uri_segment'] = '6';
        $this->pagination->initialize($config);
        $data['make_id'] = $makeID;
        $data['model_id'] = $modelID;
        $data['version'] = $this->management_model->version_list('CAR', $per_page, $offset, $modelID);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_version', $data);
    }
//------------Version Insert--------------
    function version_insert() {
        if ($this->input->post('insert')) {
            $version_detail = $_POST['version'];
            $model_id = $_POST['model_id'];
            $company_id = $_POST['company_id'];

            $this->form_validation->set_rules('version', 'version', 'required|xss_clean|is_unique[version.version]');
            if ($this->form_validation->run()) {
                if ($version_detail != 'Enter Version') {
                    $this->management_model->version_insert('CAR', $version_detail, $model_id, $company_id);
                }
                else
                    $this->session->set_flashdata('message', 'Type a Version Name');
            } else {
                $this->session->set_flashdata('message', 'Version already exists');
            }
        }
        redirect('admin/management/version/' . $company_id . '/' . $model_id);
    }
//------------Version update--------------
    function version_update() {
        $version_id = $_POST['version_id'];
        $model_id = $_POST['model_id'];
        $company_id = $_POST['make_id'];
        $version_detail = $_POST['version_detail'];
        $this->form_validation->set_rules('version_id', 'version_id', 'required|xss_clean');
        $this->form_validation->set_rules('version_detail', 'version_detail', 'required|xss_clean');
        if ($this->form_validation->run()) {
            $this->management_model->version_update('CAR', $version_id, $version_detail);
        }
        redirect('admin/management/version/' . $company_id . '/' . $model_id);
    }
//------------Version delete--------------
    function version_delete() {
        $version_id = $this->uri->segment(6);
        $model_id = $this->uri->segment(5);
        $company_id = $this->uri->segment(4);

        //--------delete previous logo -------
        $logo = $this->management_model->get_version_image('CAR', $version_id);

        if ($logo != '') {
            $path = BASEPATH . '../assets/images/data/car/version/' . $logo;
            unlink($path);
        }

        $this->management_model->version_delete('CAR', $version_id);
        redirect('admin/management/version/' . $company_id . '/' . $model_id);
    }
//------------Image of a pericluer version form--------------
    function version_image_uplaod($make_id, $model_id, $version_id) {
        $data['version']['info'] = $this->management_model->get_version('CAR', $version_id);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_version_image_upload', $data);
    }
//------------Image of a pericluer version upload--------------
    public function version_image() {
        $make_id = $_POST['make_id'];
        $model_id = $_POST['model_id'];
        $version_id = $_POST['version_id'];
        $version_detail = strtolower($this->management_model->get_version('CAR', $version_id)->version);
        $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['versionimage']['name']);
        $imagename = url_title($version_detail, '_') . '.' . $ext;
        $filetype = $_FILES['versionimage']['type'];

        if ($_FILES['versionimage']['error'] == 0 && (($filetype == 'image/jpeg') || ($filetype == 'image/jpg') || ($filetype == 'image/png') || ($filetype == 'image/gif'))) {

            //---------upload configuration--------------
            $config['file_name'] = $imagename;
            $config['upload_path'] = './assets/images/data/car/version/';
            $config['allowed_types'] = 'gif|png|jpeg|jpg';
            $config['overwrite'] = true;
            $config['remove_spaces'] = true;
            //--------delete previous logo -------
            $logo = $this->management_model->get_version_image('CAR', $version_id);

            if ($logo != '') {
                $path = BASEPATH . '../assets/images/data/car/version/' . $logo;
                unlink($path);
            }
            //--------update record-----------
            $data = array('image' => $imagename);
            $this->db->where('id', $version_id);
            $this->db->where('type', 'CAR');
            $this->db->update('version', $data);
            //----upload logo---------------
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload('versionimage')) {
                $this->session->set_flashdata('message', $this->upload->display_errors('<p class="error">', '</p>'));
            }
            redirect('admin/management/version_image_uplaod/' . $make_id . '/' . $model_id . '/' . $version_id);
        }
        $this->session->set_flashdata('message', "No file choosen! Plaese choose a file only (jpg, gif, png ,jpeg)");
        redirect('admin/management/version_image_uplaod/' . $make_id . '/' . $model_id . '/' . $version_id);
    }

//--Price and Description (Subha-07/08/2012)--//
    function version_price_description($makeID, $modelID, $versionID) {
        if ($makeID && $modelID && $versionID) {
            $data['version']['cost'] = $this->management_model->get_version('CAR', $versionID);
            $data['body_type'] = $this->pickup_model->get_pickup(3);
            $data['fuel_type'] = $this->pickup_model->get_pickup(1);
            $data['transmission'] = $this->pickup_model->get_pickup(2);
            $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                    ->set_partial('admin_header', 'header/admin_header')
                    ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                    ->build('pages/admin_version_price_description', $data);
        }
    }

    //--Price and Description Update(Subha-07/08/2012)--//
    function version_price_description_update($makeID, $modelID, $versionID) {
        $this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('message', 'Invalid Input !');
            redirect('admin/management/version_price_description/' . $makeID . '/' . $modelID . '/' . $versionID);
        } else {
            $post_data = array('make_id' => $makeID,
                'model_id' => $modelID,
                'id' => $versionID,
                'price' => $_POST['price'],
                'detail' => $_POST['description'],
                'body' => $_POST['body'],
                'fuel' => $_POST['fuel'],
                'transmission' => $_POST['transmission']
            );
            $this->management_model->version_description_update('CAR', $post_data);
            $this->session->set_flashdata('message', 'Updated successfully !');
            redirect('admin/management/version_price_description/' . $makeID . '/' . $modelID . '/' . $versionID);
        }
    }

    //--Features (Subha-03/08/2012) --?
    function version_feature($makeID, $modelID, $versionID) {
        if ($makeID && $modelID && $versionID) {
            $data['make_id'] = $makeID;
            $data['model_id'] = $modelID;
            $data['version_id'] = $versionID;
            $data['feature_interior'] = $this->management_model->get_features('CAR', 1);
            $data['feature_comfort'] = $this->management_model->get_features('CAR', 2);
            $data['feature_exterior'] = $this->management_model->get_features('CAR', 3);
            $data['feature_safety'] = $this->management_model->get_features('CAR', 4);
            $data['interior'] = $this->management_model->get_interiour_by_id('CAR', 1, $versionID);
            $data['comfort'] = $this->management_model->get_interiour_by_id('CAR', 2, $versionID);
            $data['exterior'] = $this->management_model->get_interiour_by_id('CAR', 3, $versionID);
            $data['safety'] = $this->management_model->get_interiour_by_id('CAR', 4, $versionID);
            $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                    ->set_partial('admin_header', 'header/admin_header')
                    ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                    ->build('pages/admin_version_feature', $data);
        }
    }

    //-- Joy (13.9.12) removed car_ fron function name --//
    function version_feature_insert() {
        if ($_POST['insert_feature']) {
            if ($this->input->post('version_feature') == '') {
                $this->session->set_flashdata('message', 'Please choose a feature');
                redirect('admin/management/version_feature/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
            } else {
                $this->form_validation->set_rules('version_feature', 'version_feature', 'trim|required|callback__check_duplicate_feature[version_feature]');

                if ($this->form_validation->run()) {
                    $spcode = explode(',', $this->input->post('version_feature'));
                    $code = $spcode[1];
                    $slno = $spcode[0];
                    $data = array('type' => "CAR",
                        'make' => $this->input->post('make_id'),
                        'model' => $this->input->post('model_id'),
                        'version' => $this->input->post('version_id'),
                        'code' => $code,
                        'slno' => $slno
                    );
                    $this->db->insert('features_master', $data);
                    redirect('admin/management/version_feature/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
                } else {
                    $this->session->set_flashdata('message', 'Feature already exists');
                    redirect('admin/management/version_feature/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
                }
            }
        }
    }
//-----------callback to check the duplicate feature in table----------
    function _check_duplicate_feature($param = NULL) {
        $spcode = explode(',', $param);
        $code = $spcode[1];
        $slno = $spcode[0];
        $sql = "SELECT count(*)as total FROM `features_master` WHERE code='$code' and slno=$slno and type='CAR'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($row->total != 0)
            return FALSE;
        else
            return TRUE;
    }

    function delete_features($feature_id, $make_id, $model_id, $version_id) {
        $this->db->where('slno', $feature_id);
        $this->db->delete('features_master');
        redirect('admin/management/version_feature/' . $make_id . '/' . $model_id . '/' . $version_id);
    }

    //--Specification (Joy-13/09/2012) --//
    function version_specification($makeID, $modelID, $versionID) {
        if ($makeID && $modelID && $versionID) {
            $data['make_id'] = $makeID;
            $data['model_id'] = $modelID;
            $data['version_id'] = $versionID;
            $data['specification'] = $this->management_model->get_specifications('CAR');
            $data['version_specification_code'] = $this->management_model->get_specification_code_by_version_id('CAR', $versionID);
            $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                    ->set_partial('admin_header', 'header/admin_header')
                    ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                    ->build('pages/admin_version_specification', $data);
        }
    }
//-------------Version Specification Insert
    
    function version_specification_insert() {
        if ($_POST['insert_specification']) {
            if ($this->input->post('specification') == '') {
                $this->session->set_flashdata('message', 'Please choose a Specification');
                redirect('admin/management/version_specification/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
            } else {
                $this->form_validation->set_rules('specification', 'specification', 'trim|required|callback__check_duplicate_specification[specification]');
                $this->form_validation->set_rules('specification_value', 'specification_value', 'trim');
                if ($this->form_validation->run()) {
                    $spcode = explode(',', $this->input->post('specification'));
                    $code = $spcode[1];
                    $slno = $spcode[0];
                    $data = array('type' => "CAR",
                        'make' => $this->input->post('make_id'),
                        'model' => $this->input->post('model_id'),
                        'version' => $this->input->post('version_id'),
                        'code' => $code,
                        'slno' => $slno,
                        'specification_value' => $this->input->post('specification_value')
                    );
                    $this->db->insert('specification_master', $data);
                    redirect('admin/management/version_specification/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
                } else {
                    $this->session->set_flashdata('message', 'Specification already exists');
                    redirect('admin/management/version_specification/' . $this->input->post('make_id') . '/' . $this->input->post('model_id') . '/' . $this->input->post('version_id'));
                }
            }
        }
    }
//-----------callback to check the duplicate specification in table----------
    function _check_duplicate_specification($slno = NULL) {
        $spcode = explode(',', $slno);
        $code = $spcode[1];
        $slno = $spcode[0];
        $sql = "SELECT count(*)as total FROM `specification_master` WHERE code='$code' and slno=$slno and type='CAR'";
        $query = $this->db->query($sql);
        $row = $query->row();
        if ($row->total != 0)
            return FALSE;
        else
            return TRUE;
    }
//-----------Delete perticuler specification  from a version specification ---------------
    function delete_specification($code, $slno, $make_id, $model_id, $version_id) {
        $this->management_model->delete_specification_value($code, $slno);
        redirect('admin/management/version_specification/' . $make_id . '/' . $model_id . '/' . $version_id);
    }
}
?>